package com.example.mybatis.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatis.Pojo.Exam;
import com.example.mybatis.Service.ExamService;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "http://localhost:8081")
@RestController
public class ExamController {

    @Autowired
    private ExamService examService;

    //获取成绩信息接口
    @RequestMapping("/exam")
    public String examdata(@RequestBody Map<String, String> data) throws InterruptedException, JSONException {
        String username = data.get("username");
        String password = data.get("password");
        //通过username删除原有数据库，做更新操作
        examService.remove(new QueryWrapper<Exam>().eq("username", username));
        //加载驱动
        System.setProperty("webdriver.chrome.driver", "D:\\a\\chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
        //解决打开chrome浏览器data;,异常
        chromeOptions.addArguments("--remote-allow-origins=*");
        // 打开chrome浏览器
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.manage().window().maximize();
        // 进入石河子大学教务系统登录网址
        driver.get("http://authserver.shzu.edu.cn/authserver/login?service=https%3A%2F%2Fjwgl.shzu.edu.cn%2Fsso.jsp");
        Thread.sleep(1000);
        //输入学号，密码
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.tagName("button")).click();
        Thread.sleep(1000);
        driver.get("https://jwgl.shzu.edu.cn/jsxsd/xsks/xsksap_query");
        Thread.sleep(1000);
        driver.findElement(By.xpath("//*[@id=\"btn_query\"]")).click();
        Thread.sleep(2000);

        // 切换到id为fcenter的iframe
        WebElement iframe = driver.findElement(By.id("fcenter"));
        driver.switchTo().frame(iframe);

        // 获取table元素
        WebElement table = driver.findElement(By.tagName("table"));

        // 遍历表格数据
        List<WebElement> rows = table.findElements(By.tagName("tr"));
        if (rows.isEmpty()) {
            System.out.println("没有获取到数据行");
        }
        for (int i = 1; i < rows.size(); i++) {
            JSONObject jsonObject = new JSONObject();
            List<WebElement> cells = rows.get(i).findElements(By.tagName("td"));
            for (int j = 0; j < cells.size(); j++) {
                jsonObject.put("col" + j, cells.get(j).getText());
            }
            //存储数据库
            Exam exam = new Exam();
            exam.setUsername(username);
            exam.setId(jsonObject.getInt("col0"));
            exam.setKsxq(jsonObject.getString("col1"));
            exam.setKcmc(jsonObject.getString("col5"));
            exam.setKssj(jsonObject.getString("col7"));
            exam.setKc(jsonObject.getString("col8"));
            //存储数据库
            examService.save(exam);
        }

        return "success";
    }

    //从数据库拿对应username的考试数据
    @ResponseBody
    @GetMapping("/examImpl/{username}")
    public List<Exam> getScoreListByUsername(@PathVariable String username) {
        return examService.getExamListByUsername(username);
    }

}
